import org.apache.commons.io.FileUtils;
import org.openqa.selenium.*;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;
import org.openqa.selenium.support.ui.ExpectedCondition;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;

import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.time.Duration;
import java.util.List;
import java.util.Set;

public class test {
    public void test01() {

        //驱动程序管理的⾃动化
        System.setProperty("webdriver.chrome.driver", "D:\\qq\\chromedriver-win64(1)\\chromedriver-win64\\chromedriver.exe");

        ChromeOptions options = new ChromeOptions();
        //允许访问所有链接
        options.addArguments("--remote-allow-origins=*");
        //1、打开浏览器
        WebDriver driver = new ChromeDriver(options);
        //2.输⼊百度⽹址:https://www.baidu.com
        driver.get("https://www.baidu.com");


        //元素的定位
        //cssSelector
//        //查找百度输入框
//        driver.findElement(By.cssSelector("#chat-textarea"));
//
//        //查找百度按钮
//        driver.findElement(By.cssSelector("#chat-submit-button"));


//---------------------------------------------------------------------------------------------------

        //xpath
        //查找百度输入框
        //driver.findElement(By.xpath("//*[@id=\"chat-textarea\"]"));
        //查找百度按钮
        //driver.findElement((By.xpath("//*[@id=\"chat-submit-button\"]")));

//---------------------------------------------------------------------------------------------------

        //查找findements
        //List<WebElement>eles=driver.findElements(By.cssSelector("#hotsearch-content-wrapper>li"));
        List<WebElement> eles = driver.findElements(By.xpath("//*[@id=\"hotsearch-content-wrapper\"]/li/a/span[2]"));
        for (WebElement e : eles) {
            System.out.println(e.getText());
        }

        driver.quit();
    }

    public  void test02() throws InterruptedException {
        //驱动程序管理的⾃动化
        System.setProperty("webdriver.chrome.driver", "D:\\qq\\chromedriver-win64(1)\\chromedriver-win64\\chromedriver.exe");

        ChromeOptions options = new ChromeOptions();
        //允许访问所有链接
        options.addArguments("--remote-allow-origins=*");
        //1、打开浏览器
        WebDriver driver = new ChromeDriver(options);
        //2.输⼊百度⽹址:https://www.baidu.com
        driver.get("https://www.baidu.com");

//对元素的操作
        //输入框---输入文本信息--sendkeys
//        WebElement ele=driver.findElement(By.cssSelector("#chat-textarea"));
//        ele.sendKeys("ndhuahdsh");


        //合并成一句
        //driver.findElement(By.cssSelector("#chat-textarea")).sendKeys("djkjbsb");

//---------------------------------------------------------------------------------------------------


        //按钮--点击--click
        // WebElement ele=driver.findElement(By.cssSelector("#chat-submit-button"));
        //ele.click();
        //合并成一句
        // driver.findElement(By.cssSelector("#chat-submit-button")).click();;
        // Thread.sleep(3000);

//---------------------------------------------------------------------------------------------------

        //查找百度输入框---更改输入内容
//        WebElement ele=driver.findElement(By.cssSelector("#chat-textarea"));
//        //输入迪丽热巴
//        ele.sendKeys("迪丽热巴");
//        Thread.sleep(2000);
//
//        //清空输入框
//        ele.clear();
//        Thread.sleep(2000);
//
//
//        //输入邓紫棋
//        ele.sendKeys("邓紫棋" );
//        Thread.sleep(2000);
//        driver.quit();

//---------------------------------------------------------------------------------------------------

//        //获取新闻超链接上的文本信息
//        String newsText=driver.findElement(By.cssSelector("#s-top-left > a:nth-child(1)")).getText();
//        System.out.println("nextText:"+newsText);
//
//        //获取百度一下按钮上的文本信息
//        //String baiduText=driver.findElement(By.xpath("//*[@id=\"chat-submit-button\"]")).getText();
//        //获取属性值
//        String baiduText=driver.findElement(By.xpath("//*[@id=\"chat-submit-button\"]")).getAttribute("value");
//        System.out.println("baiduText:"+baiduText);

        //---------------------------------------------------------------------------------------------------

        //获取跳转之前标题和URL
        String titleBefore = driver.getTitle();
        String currentUrlBefore = driver.getCurrentUrl();

        System.out.println("titleBefore:" + titleBefore);
        System.out.println("urlBefore" + currentUrlBefore);

        System.out.println();


        //百度首页点击“图片”超链接，跳转到图片页面
        driver.findElement(By.cssSelector("#s-top-left > a:nth-child(6)")).click();
        Thread.sleep(2000);


        //百度首页页面的句柄和百度图片的句柄
        String curHandle = driver.getWindowHandle();//百度首页页面的句柄
        Set<String> allHanlds = driver.getWindowHandles(); //百度首页页面的句柄和百度图片的句柄
        //driver切换到百度图片标签页
        for (String handle : allHanlds) {
            if (!handle.equals(curHandle))//如果不是百度首页页面的句柄，进行切换
            {
                //切换
                driver.switchTo().window(handle);
            }
        }

        //获取跳转之后的标题和URL
        String titleAfter = driver.getTitle();
        String currentUrlAfter = driver.getCurrentUrl();

        System.out.println("titleAfter:" + titleAfter);
        System.out.println("urlAfter" + currentUrlAfter);
        driver.quit();
    }

    //屏幕截图时间
    public void getScreenShot(String str, WebDriver driver) throws IOException {
        SimpleDateFormat sim1 = new SimpleDateFormat("yyyy-MM-dd");
        SimpleDateFormat sim2 = new SimpleDateFormat("HHmmssSS");

        //文件夹的时间
        String dirTime = sim1.format(System.currentTimeMillis());
        //文件的时间
        String fileTime = sim2.format(System.currentTimeMillis());

//文件夹的名称
        String filename = "./src/test/images/" + dirTime + "/" + str + "-" + fileTime + ".png";

        File srcfile = ((TakesScreenshot) driver).getScreenshotAs(OutputType.FILE);
        //file---->.png file
        FileUtils.copyFile(srcfile, new File(filename));//截图会覆盖

        //文件的名称

    }

    public void test03() throws InterruptedException, IOException {
        //驱动程序管理的⾃动化
        System.setProperty("webdriver.chrome.driver", "D:\\qq\\chromedriver-win64(1)\\chromedriver-win64\\chromedriver.exe");

        ChromeOptions options = new ChromeOptions();
        //允许访问所有链接
        options.addArguments("--remote-allow-origins=*");
        //1、打开浏览器
        WebDriver driver = new ChromeDriver(options);


//        //最大化
//        driver.manage().window().maximize();
//        Thread.sleep(3000);
//        //最小化
//        driver.manage().window().minimize();
//        Thread.sleep(3000);
//        //全屏
//        driver.manage().window().fullscreen();
//        Thread.sleep(3000);
//        //手动设置大小
//        driver.manage().window().setSize(new Dimension(1024,700));
//        Thread.sleep(3000);


        //2.输⼊百度⽹址:https://www.baidu.com
        driver.get("https://www.baidu.com");

//        //屏幕截图把页面加载后的情况拍照留存下来
//        File srcfile=((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
//        //file---->.png file
//        FileUtils.copyFile(srcfile,new File("test.png"));//截图会覆盖

        getScreenShot(Thread.currentThread().getStackTrace()[1].getMethodName(), driver);
        driver.quit();

    }

    public void test04() throws InterruptedException {
        //驱动程序管理的⾃动化
        System.setProperty("webdriver.chrome.driver", "D:\\qq\\chromedriver-win64(1)\\chromedriver-win64\\chromedriver.exe");

        ChromeOptions options = new ChromeOptions();
        //允许访问所有链接
        options.addArguments("--remote-allow-origins=*");
        //1、打开浏览器
        WebDriver driver = new ChromeDriver(options);

        //2.输⼊百度⽹址:https://www.baidu.com
        driver.get("https://www.baidu.com");

        //点击百度首页，打开第二个标签页
//        driver.findElement(By.cssSelector("#s-top-left > a:nth-child(6)")).click();
//        Thread.sleep(3000);
        //driver.close();//关闭百度首页标签页


        //隐式等待
        //driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(3));

        //显式等待
        WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(3));

        //等待输入框元素出现
        wait.until(ExpectedConditions.presenceOfElementLocated(By.cssSelector("#kw")));

        //等待“百度一下”按钮可以点击
        wait.until(ExpectedConditions.elementToBeClickable(By.cssSelector("#su")));

        //等待url一不一样
        wait.until(ExpectedConditions.urlToBe("https://www.baidu.com/"));


        //查找百度输入框
        //driver.findElement(By.cssSelector("#chat-textarea")).sendKeys("迪丽热巴");
        //driver.findElement(By.cssSelector("#kw")).sendKeys("迪丽热巴");
        //查找百度按钮
        //driver.findElement(By.cssSelector("#chat-submit-button")).click();
        //driver.findElement(By.cssSelector("#su")).click();

        // driver.findElement(By.cssSelector("#\\31  > div > div > div > div > div > div.new-tag_4ozgi.new-text-link_3k9GD > div > div.flex-wrapper-top_3ucFS > div.flex-col-left_3trtY.baike-wrapper_6AORN.cu-pt-xs-lg.baike-wrapper-pc_26R04.cu-pt-xl.baike-wrapper-left-pc_5eYY8.cos-space-pb-sm > div > div > p > span:nth-child(1) > span"));
        driver.quit();//关闭所有标签页
    }

    public void test05() {
        //驱动程序管理的⾃动化
        System.setProperty("webdriver.chrome.driver", "D:\\qq\\chromedriver-win64(1)\\chromedriver-win64\\chromedriver.exe");

        ChromeOptions options = new ChromeOptions();
        //允许访问所有链接
        options.addArguments("--remote-allow-origins=*");
        //1、打开浏览器
        WebDriver driver = new ChromeDriver(options);

        //2.输⼊百度⽹址:https://www.baidu.com
        driver.get("https://www.baidu.com");

        //隐式等待设置为5s，显⽰等待设置为10s，那么结果会是5+10=15s吗？

        SimpleDateFormat sim = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        System.out.println(sim.format(System.currentTimeMillis()));

        //隐式等待---5s
        driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(5));
        //显示等待---10s
        WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(10));
        try {
            wait.until(ExpectedConditions.presenceOfElementLocated(By.cssSelector("#hotsear ch-content-wrapper > li:nth-child(1) > a > span.title-content")));
        } catch (Exception e) {
            System.out.println("nosuelement!");
        }
        System.out.println(sim.format(System.currentTimeMillis()));



    }
public  void test06() throws InterruptedException {
    //驱动程序管理的⾃动化
    System.setProperty("webdriver.chrome.driver", "D:\\qq\\chromedriver-win64(1)\\chromedriver-win64\\chromedriver.exe");

    ChromeOptions options = new ChromeOptions();
    //允许访问所有链接
    options.addArguments("--remote-allow-origins=*");
    //1、打开浏览器
    WebDriver driver = new ChromeDriver(options);

    driver.navigate().to("https://tool.lu/");
    Thread.sleep(2000);
    //点击在原页面跳转--码库
    driver.findElement(By.cssSelector("#nav > div > div > ul > li:nth-child(4) > a")).click();


    //后退--工具
    driver.navigate().back();
    Thread.sleep(2000);


    //前进--码库
driver.navigate().forward();
    Thread.sleep(2000);



    //刷新--工具
    driver.navigate().refresh();
    Thread.sleep(2000);
    //driver.quit();
}

public  void test07() throws InterruptedException {
    //驱动程序管理的⾃动化
    System.setProperty("webdriver.chrome.driver", "D:\\qq\\chromedriver-win64(1)\\chromedriver-win64\\chromedriver.exe");

    ChromeOptions options = new ChromeOptions();
    //允许访问所有链接
    options.addArguments("--remote-allow-origins=*");
    //1、打开浏览器
    WebDriver driver = new ChromeDriver(options);

    //1.获取文本
//    //请求url
//    driver.get("file:///D:/%E6%B5%8B%E8%AF%95/selenium-html/Prompt.html");
//
//    //点击掉起弹窗
//    driver.findElement(By.cssSelector("body > input[type=button]")).click();
//    Thread.sleep(3000);
//
//    //切换弹窗
//    Alert alert=driver.switchTo().alert();
//    Thread.sleep(3000);
//
//
//    System.out.println("text"+alert.getText());
//
//
//    //alert.dismiss();//点击取消
//    alert.sendKeys("lll");
//    Thread.sleep(3000);
//
//    //点击确认
//    alert.accept();
//    Thread.sleep(3000);


//-----------------------------------------------------------------------------------

//    //警告弹窗----只有一个按钮，取消和确认的效果一样
//    driver.get("file:///D:/%E6%B5%8B%E8%AF%95/selenium-html/alert.html");
//    //点击掉起弹窗
//    driver.findElement(By.cssSelector("#tooltip")).click();
//    Thread.sleep(2000);
//
//    Alert alert=driver.switchTo().alert();
//    Thread.sleep(2000);
//    //点击确认
//    alert.accept();
//    //点击取消
//    alert.dismiss();
//    Thread.sleep(2000);

    //-----------------------------------------------------------------------------------


    //文件上传
    driver.get("file:///D:/%E6%B5%8B%E8%AF%95/selenium-html/upload.html");
    Thread.sleep(3000);

    driver.findElement(By.cssSelector("body > div > div > input[type=file]")).sendKeys("C:\\Users\\zhen\\Desktop\\java\\资料.txt");
    Thread.sleep(3000);
    driver.quit();



}


public  void test08() throws IOException {
    //驱动程序管理的⾃动化
    System.setProperty("webdriver.chrome.driver", "D:\\qq\\chromedriver-win64(1)\\chromedriver-win64\\chromedriver.exe");

    ChromeOptions options = new ChromeOptions();
    //允许访问所有链接
    options.addArguments("--remote-allow-origins=*");

//    //添加配置--无头模式
//    options.addArguments("-headless");

    //添加配置--设置浏览器加载策略
    //options.setPageLoadStrategy(PageLoadStrategy.EAGER);
   // options.setPageLoadStrategy(PageLoadStrategy.NONE);//建议不使用
    options.setPageLoadStrategy(PageLoadStrategy.NORMAL);
    //1.打开浏览器
    WebDriver driver=new ChromeDriver(options);

   driver.get("https://www.bilibili.com/");




//    //1、打开浏览器
//    WebDriver driver = new ChromeDriver(options);
//
//    //请求百度网站
//    driver.get("https://www.baidu.com");
//    //输入迪丽热巴
//    driver.findElement(By.cssSelector("#kw")).sendKeys("迪丽热巴");
//    //点击百度一下按钮
//    driver.findElement(By.cssSelector("#su")).click();
//
//    getScreenShot(Thread.currentThread().getStackTrace()[1].getMethodName(),driver);

    driver.quit();
}
}
